<?php
/**
 * Oss controller
 */
namespace App\Http\Controllers;

use App\Service\OssService;
use App\Service\ConfigsService;
use DateTime;
// use Phalcon\Http\Response;

class OssController extends BaseController
{

	public function indexAction()
	{
        if ($this->request->hasFiles()) {
        	$files_path = [];
        	$files = $_FILES;
        	$pic_path = "uploads/".date("Y/m/d");
            $doc_path = "uploads/files/".date("Y/m/d");
            $file_name = md5(uniqid());
            $file_urls = [];
            $oss = new OssService;
        	foreach ($files as $file) {
        		$type = $file['type'];
        		$file_suffix = explode('.', $file['name']);
        		$file_suffix = !empty(end($file_suffix)) ? end($file_suffix) : 'png';
     			$temp_name = $file['tmp_name'];
        		$key = $pic_path . '/' . $file_name . '.' . $file_suffix;

        		// $file_urls[] = $oss->multiuploadFile($temp_name, $key);
				$file_urls[] = $oss->uploadFile($temp_name, $key);
			}
			return $this->echoJson($file_urls);
		}	
		return $this->echoJson('Files Not Find',401);
	}

	/**
	 * [gmt_iso8601 description]
	 * @param  [type] $time [description]
	 * @return [type]       [description]
	 */
	protected function gmt_iso8601($time) {
        $dtStr = date("c", $time);
        $mydatetime = new DateTime($dtStr);
        $expiration = $mydatetime->format(DateTime::ISO8601);
        $pos = strpos($expiration, '+');
        $expiration = substr($expiration, 0, $pos);
        return $expiration."Z";
    }

    public function configAction()
    {
    	$config  = new ConfigsService;
    	$id      = $this->config->oss->aliyuncs->accessKeyId;
    	$key     = $this->config->oss->aliyuncs->accessKeySecret;
    	$host    = $this->config->oss->aliyuncs->host;
    	$oss_url = $this->config->oss->aliyuncs->oss_url;
    	$res_url = $this->config->oss->aliyuncs->res_url;
    	$bucket_url = $host;
    	$arr = new \stdClass();
    	$arr->name = "file_suffix";
    	$files_type = implode(",", ConfigsService::getVal($arr));
    	$max_file_size  = "1000mb";

	    $now = time();
	    $expire = 60000; //设置该policy超时时间是10s. 即这个policy过了这个有效时间，将不能访问
	    $end = $now + $expire;
	    $expiration = $this->gmt_iso8601($end);

	    $dir = "uploads/".date("Y/m/d/");

	    //最大文件大小.用户可以自己设置1000mb
	    $condition = array(0=>'content-length-range', 1=>0, 2=>1048576000);
	    $conditions[] = $condition; 

	    //表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
	    $start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
	    $conditions[] = $start; 

	    $arr = array('expiration'=>$expiration,'conditions'=>$conditions);

	    $policy = json_encode($arr);
	    $base64_policy = base64_encode($policy);
	    $string_to_sign = $base64_policy;
	    $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));

	    $response = array();
	    $response['accessid'] = $id;
	    $response['host'] = $host;
	    $response['policy'] = $base64_policy;
	    $response['signature'] = $signature;
	    $response['expire'] = $end;
	    //这个参数是设置用户上传指定的前缀(目录)
	    $response['dir'] = $dir;
	    $response['max_file_size'] = $max_file_size;//文件大小
	    $response['files_type'] = $files_type;//支持文件类型
	    $response['oss_url'] = $oss_url;//阿里云oss服务器url
	    $response['bucket_url'] = $host;//阿里云bucket_url
	    $response['res_url'] = $res_url;//阿里云bucket_url
	    $response['file_name'] = md5(uniqid());//文件名 
	    //跨域
	    header('content-type:application:json;charset=utf8');  
		header('Access-Control-Allow-Origin:*');  
		header('Access-Control-Allow-Methods:POST');  
	    header('Access-Control-Allow-Headers:x-requested-with,content-type'); 
	    return json_encode($response);
    }
}